//
//  VisitorLoginView.swift
//  AAWeibo
//
//  Created by 上官风雨 on 15/11/11.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate: NSObjectProtocol{
    
    func willRegister()
    func willLogin()
    
}

class VisitorLoginView: UIView {
    
    var visitorDelegate: VisitorLoginViewDelegate?

    override init(frame: CGRect) {
        
        super.init(frame: CGRectZero)
        
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //代理方法
    func loginDidClick(){
        
        visitorDelegate?.willLogin()
    }
    //代理方法
    func registerDidClick(){
        
        visitorDelegate?.willRegister()
    }
    
    func setUIInfo(title:String?,imageName:String?){
        
        backView.hidden = true
        circleView.hidden = true
        tipLabel.text = title
        houseView.image = UIImage(named: imageName!)
        if title == nil {
            tipLabel.text = "关注一些人，回这里看看有什么"
            backView.hidden = false
            circleView.hidden = false
            startAni()
        }
    
    }
    
    func startAni(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 9
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    func setUI(){
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(houseView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        for v in subviews{
            
            v.translatesAutoresizingMaskIntoConstraints = false
            
        }
        
        //动态图的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //小房子的约束
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //背景图的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backview]-0-|", options: [], metrics: nil, views: ["backview":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backview]-(-35)-[loginBtn]", options: [], metrics: nil, views: ["backview":backView,"loginBtn":loginBtn]))
        
        //提示文字的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 255))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        //登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        //注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        
           loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    
    
    //懒加载动态图片
    lazy var circleView :UIImageView =  UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //懒加载小房子图片
    lazy var houseView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var backView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //懒加载提示文字
    lazy var tipLabel : UILabel = {
        
        var tipLabel = UILabel()
        tipLabel.numberOfLines = 0
        tipLabel.text = "关注一些人，回这里看看有什么"
        tipLabel.font = UIFont.systemFontOfSize(14)
        tipLabel.textAlignment = NSTextAlignment.Center
        
        return tipLabel
    }()
    
    //懒加载登录按钮
    lazy var loginBtn : UIButton = {
        
        var loginBtn = UIButton()
       
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
     
        
        return loginBtn
    }()
    
    //懒加载注册按钮
    lazy var registerBtn : UIButton = {
        
        var registerBtn = UIButton()
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        registerBtn.setTitle("注册", forState: .Normal)
        
        registerBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
        
        return registerBtn
    }()
 
}
